<?php
namespace Core\Service;

use Phalcon\Di\Injectable;
use Phalcon\Validation AS PhalconValidation;

class Validation extends Injectable
{
    public function register() {
        $this->di->setShared('validation', new class extends PhalconValidation {

            public function push()
            {

            }

            public function verify($rules = null, $data = null)
            {
                if (empty($rules)){
                    return false;
                }
                foreach($rules AS $rule)
                {
                    if (gettype($rule) == 'string'){
                        $this->add($rule, new \Phalcon\Validation\Validator\PresenceOf(array(
                            'message' => 'The '.$rule.' is required.'
                        )));
                    }else{
                        if (!isset($rule[0])) continue;
                        if (count($rule) == 2){
                            $field = $rule[0];
                            $type = 'PresenceOf';
                            $param = ['message'=>$rule[1]];
                        }else if (count($rule) == 3){
                            $field = $rule[0];
                            $type = isset($rule[1]) ? $rule[1] : 'PresenceOf';
                            $param = isset($rule[2]) ? $rule[2] : [];
                        }
                        $class = '\Phalcon\Validation\Validator\\' . $type;
                        $this->add($field, new $class($param));
                    }
                }
                $this->validate($data);
                return $this->getMessages();
            }

        });
        return $this;
    }
}